#include "stdafx.h"
#include <stdio.h>
#include <time.h>

BOOL CheckWinFileDate(const char *suffix)
{
	char lpWindowsPath[MAX_PATH];
	char lpListPath[MAX_PATH+20];
	FILETIME	ftSystemTime;
	WIN32_FIND_DATA wfdIni;
	HANDLE		hdIniFile;
	FILETIME	ftCreation, ftLastAccess, ftLastWrite;

	GetSystemTimeAsFileTime(&ftSystemTime);
	GetWindowsDirectory(lpWindowsPath, MAX_PATH);
	strcpy(lpListPath, lpWindowsPath);
	strcat(lpListPath, "\\*.");
	strcat(lpListPath, suffix);
	HANDLE hdFindIni=FindFirstFile(lpListPath, &wfdIni);
	if(hdFindIni==INVALID_HANDLE_VALUE) {
		//printf("Cannot list %s!\n", lpListPath); // !!!
		return(TRUE);
	}

	int validSystemTime=TRUE;
	while(FindNextFile(hdFindIni, &wfdIni)) {
		strcpy(lpListPath, lpWindowsPath);
		strcat(lpListPath, "\\");
		strcat(lpListPath, wfdIni.cFileName);
		//printf("checking %s..", lpListPath);
		if((hdIniFile=CreateFile(lpListPath, GENERIC_READ, FILE_SHARE_READ, NULL, 
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {
			GetFileTime(hdIniFile, &ftCreation, &ftLastAccess, &ftLastWrite);
			CloseHandle(hdIniFile);
			if(CompareFileTime(&ftSystemTime, &ftLastWrite) < 0) {
				//printf("Invalid date!\n"); // !!!
				validSystemTime=FALSE;
				break;
			}
			else {
				//printf("Ok.\n");
			}
		}
		else {
			//printf("not permitted!\n");
		}
	}
	FindClose(hdFindIni);
	return(validSystemTime);
}

BOOL CheckWinDate(void)
{
	if(!CheckWinFileDate("ini")) {
		return(FALSE);
	}
	else if(!CheckWinFileDate("xml")) {
		return(FALSE);
	}
	else if(!CheckWinFileDate("log")) {
		return(FALSE);
	}
	else {
		return(TRUE);
	}
}

int GetLocalDate(void)
{
    time_t      ttDate;
    struct tm   *tmStru;

    ttDate=time(NULL);
    tmStru=localtime(&ttDate);
    return((tmStru->tm_year%100)*100+tmStru->tm_mon+1);
}

